### packages
library(cjoint)
library(foreign)
library(dplyr)
library(cregg)
library(ggplot2)
library(patchwork)
library(labelled)

########################
# Load and set up data #
########################
## France
# load recoded data
dat.e2fr <- read.csv("France - cjoint 2.csv")
# organize factor variable order
dat.e2fr$local <- factor(dat.e2fr$local, levels = c("More national","More local"))
dat.e2fr$discipline <- factor(dat.e2fr$discipline, levels = c("More discipline","More freedom"))
dat.e2fr$selection <- factor(dat.e2fr$selection, levels = c("More centralization","More decentralization"))
dat.e2fr$funds <- factor(dat.e2fr$funds, levels = c("More donors","More member dues"))
dat.e2fr$women <- factor(dat.e2fr$women, levels = c("More quotas","No quotas"))

## Israel
## load recoded data 
dat.e2il <- read.csv("Israel - cjoint 2.csv")
## organize factor variable order
dat.e2il$local <- factor(dat.e2il$local, levels = c("More national","More local"))
dat.e2il$discipline <- factor(dat.e2il$discipline, levels = c("More discipline","More freedom"))
dat.e2il$selection <- factor(dat.e2il$selection, levels = c("More centralization","More decentralization"))
dat.e2il$funds <- factor(dat.e2il$funds, levels = c("More donors","More member dues"))
dat.e2il$women <- factor(dat.e2il$women, levels = c("More quotas","No quotas"))

## Sweden
## load recoded data 
dat.e2sw <- read.csv("Sweden - cjoint 2.csv")
## organize factor variable order
dat.e2sw$local <- factor(dat.e2sw$local, levels = c("More national","More local"))
dat.e2sw$discipline <- factor(dat.e2sw$discipline, levels = c("More discipline","More freedom"))
dat.e2sw$selection <- factor(dat.e2sw$selection, levels = c("More centralization","More decentralization"))
dat.e2sw$funds <- factor(dat.e2sw$funds, levels = c("More donors","More member dues"))
dat.e2sw$women <- factor(dat.e2sw$women, levels = c("More quotas","No quotas"))


#################################
# estimation - AMCE (Figure B4) #
#################################
## perform estimates 
out.amce.fr2 <- cj(data = dat.e2fr,
                   selected ~ local + discipline + selection + funds + women,
                   id = ~Response.ID,
                   estimate = "amce",
)
out.amce.il2 <- cj(data = dat.e2il,
                   selected ~ local + discipline + selection + funds + women,
                   id = ~Response.ID,
                   estimate = "amce",
)
out.amce.sw2 <- cj(data = dat.e2sw,
                   selected ~ local + discipline + selection + funds + women,
                   id = ~Response.ID,
                   estimate = "amce",
)
## plot outcomes
# define attribute order for graphs
out.amce.fr2$feature <- factor(out.amce.fr2$feature, 
                               levels = c("selection","local","discipline","funds","women"))
out.amce.il2$feature <- factor(out.amce.il2$feature, 
                               levels = c("selection","local","discipline","funds","women"))
out.amce.sw2$feature <- factor(out.amce.sw2$feature, 
                               levels = c("selection","local","discipline","funds","women"))
# define labels for graphs
attributes <- c("Candidae\nSelection","National \nv. Local","Partyn\nDiscipline","Party\nFunding",
                "Female\nRepresentation") 
names(attributes) <- c("selection","local","discipline","funds","women")
# plot graphs
plot.amce.fr2 <- plot(out.amce.fr2, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_manual(values = rep("black", 17)) +
  ggplot2::xlim(-0.2,0.2) +
  ggplot2::theme(legend.position = "none") +
  ggplot2::ggtitle("France")
plot.amce.il2 <- plot(out.amce.il2, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_manual(values = rep("black", 17)) +
  ggplot2::xlim(-0.2,0.2) +
  ggplot2::theme(legend.position = "none", axis.text.y=element_blank()) +
  ggplot2::ggtitle("Israel")
plot.amce.sw2 <- plot(out.amce.sw2, size=1.5, feature_headers = FALSE) + 
  ggplot2::facet_wrap(~ feature, ncol = 1L, scales = "free_y", strip.position = "right",
                      labeller = labeller(.rows = attributes)) +
  ggplot2::geom_errorbarh(ggplot2::aes_string(xmin = "lower", xmax = "upper"),  
                          size = 0.6, height = 0, na.rm = TRUE,
                          position = ggstance::position_dodgev(height = 0.75)) +
  ggplot2::scale_color_manual(values = rep("black", 17)) +
  ggplot2::xlim(-0.2,0.2) +
  ggplot2::theme(legend.position = "none", axis.text.y=element_blank()) +
  ggplot2::ggtitle("Sweden")
plot.acme.all1 <- plot.amce.fr2 + plot.amce.il2 + plot.amce.sw2 
# save file (Figure B4)
ggsave(file = "FigureB4.pdf", plot=plot.acme.all1, width=8, height=6.5)
